package com.ys.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.MakeAppInfoVo;
import com.common.vo.OrderVo;
import com.ys.data.async.AsyncService;
import com.ys.data.dao.MakeAppInfoMapper;
import com.ys.data.entry.MakeAppInfo;
import com.ys.data.entry.Order;
import com.ys.data.service.MakeAppInfoService;
import com.ys.data.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: MakeAppInfoServiceImpl
 * @packageName: MakeAppInfoServiceImpl
 * @description: 预约接口实现
 * @data: 2021-1-22
 **/
@Service
public class MakeAppInfoServiceImpl extends ServiceImpl<MakeAppInfoMapper, MakeAppInfo> implements MakeAppInfoService {

    @Resource
    private MakeAppInfoMapper makeAppInfoMapper;

    @Resource
    private OrderService orderService;

    @Resource
    @Lazy
    private AsyncService asyncService;

    /**
     * 添加预约信息
     * @param makeAppInfoVo
     * @return int
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "订单服务-添加预约信息",operationType=BaseEnum.INSERT)
    public int save(MakeAppInfoVo makeAppInfoVo) throws Exception {
        if (Objects.isNull(makeAppInfoVo)) throw new BaseException(BaseEnum.MAKE_DATA_IS_NULL);
        try {
            MakeAppInfo makeAppInfo = new MakeAppInfo();
            BeanUtils.copyProperties(makeAppInfoVo, makeAppInfo);
            int makeResult = makeAppInfoMapper.insert(makeAppInfo);
            if (makeResult > 0) {
                OrderVo orderVo = OrderVo.builder().userId(makeAppInfoVo.getUserId())
                        .makeAppId(makeAppInfo.getId()).orderStatus(makeAppInfoVo.getMakeAppStatus())
                        .placeOrderDate(makeAppInfoVo.getCreatedDate()).createdDate(LocalDateTime.now()).
                                build();
                return orderService.save(orderVo);
            }
        } catch (Exception e) {
            throw new BaseException(BaseEnum.SAVE_MAKE_DATA_IS_ERROR);
        }
        return 0;
    }

    /**
     * 更新预约信息
     * @param makeAppInfoVo
     * @return int
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "订单服务-更新预约信息",operationType=BaseEnum.UPDATE)
    public int update(MakeAppInfoVo makeAppInfoVo) throws Exception {
        if (Objects.isNull(makeAppInfoVo)) throw new BaseException(BaseEnum.MAKE_DATA_IS_NULL);
        try {
            MakeAppInfo makeAppInfo = new MakeAppInfo();
            BeanUtils.copyProperties(makeAppInfoVo, makeAppInfo);
            int makeResult = makeAppInfoMapper.updateById(makeAppInfo);
            //查询订单信息
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("make_app_id", makeAppInfoVo.getId());
            Order order = orderService.findOrder(queryWrapper);
            if (makeResult > 0) {
                int status = getOrderStatus(makeAppInfoVo.getMakeAppStatus());
                OrderVo orderVo = new OrderVo();
                orderVo.setOrderStatus(status);
                orderVo.setId(order.getId());
                orderVo.setUserId(makeAppInfoVo.getUserId());
                //订单完成
                if (status == 3) {
                    orderVo.setCompleteDate(LocalDateTime.now());
                    //异步处理积分信息
                    asyncService.initRoleDataResult(makeAppInfoVo,orderVo);
                }
                return orderService.update(orderVo);
            }
        } catch (Exception e) {
            throw new BaseException(BaseEnum.SAVE_MAKE_DATA_IS_ERROR);
        }
        return 0;
    }

    /**
     * 查询预约详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "订单服务-查询预约详情",operationType=BaseEnum.SELECT)
    public MakeAppInfo findMakeAppInfoById(Long id) throws Exception {
        if (null == id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return makeAppInfoMapper.selectById(id);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_MAKE_DATA_IS_ERROR);
        }
    }

    /**
     * 查询用户预约信息
     * @param queryWrapper
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "订单服务-查询用户预约信息",operationType=BaseEnum.SELECT)
    public List<MakeAppInfo> findMakeAppInfos(QueryWrapper<MakeAppInfo> queryWrapper) {
        if (null == queryWrapper) {
            throw new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        try {
            return makeAppInfoMapper.selectList(queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_MAKE_DATA_IS_ERROR);
        }
    }

    /**
     * 删除预约
     * @param id
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "订单服务-删除预约信息",operationType=BaseEnum.DELETE)
    public int delete(Long id) throws Exception {
        if (null == id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            int delResult = makeAppInfoMapper.deleteById(id);
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("make_app_id", id);
            Order order = orderService.findOrder(queryWrapper);
            if (delResult > 0) {
                return orderService.delete(order.getId());
            }
        } catch (Exception e) {
            throw new BaseException(BaseEnum.DELETE_MAKE_DATA_IS_ERROR);
        }
        return 0;
    }

    /**
     * 查询预约列表
     * @param page
     * @param makeAppInfoVo
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "订单服务-查询预约列表",operationType=BaseEnum.SELECT)
    public IPage<MakeAppInfo> makeAppInfoPage(Page<MakeAppInfo> page, MakeAppInfoVo makeAppInfoVo) throws Exception {
        if (Objects.isNull(makeAppInfoVo)) {
            throw new BaseException(BaseEnum.MAKE_DATA_IS_NULL);
        }
        try {
            QueryWrapper<MakeAppInfo> queryWrapper = new QueryWrapper<>();
            return makeAppInfoMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_MAKE_DATA_IS_ERROR);
        }
    }

    /**
     * 状态转换
     *
     * @param makeStatus
     * @return
     */
    private Integer getOrderStatus(Integer makeStatus) {
        if (1 == makeStatus) {
            return 2;
        } else if (2 == makeStatus) {
            return 1;
        } else if (3 == makeStatus) {
            return 3;
        } else if (4 == makeStatus) {
            return 4;
        }
        return 0;
    }
}
